package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserFlow;
import com.tebiecloud.mpweixin.server.service.MpweixinUserFlowService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.server.service.feign.OrderServiceClient;
import com.tebiecloud.mpweixin.server.service.feign.YunWithdrawServiceClient;
import com.tebiecloud.order.client.model.vo.IncomeList;
import com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户资金记录表 前端控制器
 * @author tebie
 * @date 2019-09-16
 */
@RestController
@RequestMapping("/mpweixinuserflow")
public class MpweixinUserFlowController {

    @Autowired
    private MpweixinUserFlowService mpweixinUserFlowService;

    @Autowired
    private YunWithdrawServiceClient yunWithdrawServiceClient;

    @Autowired
    private OrderServiceClient orderServiceClient;
    /**
     * 获取分页数据
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<IPage<MpweixinUserFlow>> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit) {
        return ResultBodyUtils.ok(mpweixinUserFlowService.selectFundsFlowList(page,limit));
    }

    /**
     * 获取提现详情
     * @param withdrawId
     * @return
     */
    @PostMapping("infoWithdrawInfo")
    public ResultBody infoWithdrawInfo(@RequestParam("withdrawId") Long withdrawId) {
        ResultBody<com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO> withdrawResult = yunWithdrawServiceClient.withdrawInfo(withdrawId);
        if(withdrawResult!=null && withdrawResult.getCode()== ResultEnum.OK.getCode()){
            com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO withdrawInfoVO = withdrawResult.getData();
        }else{
            return withdrawResult;
        }
        return ResultBodyUtils.ok();
    }

    /**
     * 获取收益列表
     * @param month
     * @param day
     * @return
     */
    @PostMapping("listincome")
    public ResultBody listIncome(@RequestParam(value = "month",required = false) String month,
                                 @RequestParam(value = "day",required = false) String day) {
        if(StringUtils.isEmpty(month)&&StringUtils.isEmpty(day)){
            return ResultBodyUtils.failed();
        }

        ResultBody<IncomeList> incomeListResultBody = orderServiceClient.selectIncomeList(SecurityHelper.getUserId(),month,day);
        return incomeListResultBody;
    }

}
